<template>
  <div class="todoapp">
    <todo-header @addTodo="add" :list="list" />

    <todo-main :list="filterList" @doDel="del" />

    <todo-footer
      :list="filterList"
      @changeStatus="status = $event"
      @clear="clear"
    />
  </div>
</template>

<script>
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";
export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      status: "全部",
      list: JSON.parse(localStorage.getItem("todos80")) || [],
    };
  },
  methods: {
    add(v) {
      let lastId = this.list[this.list.length - 1];
      this.list.push({
        id: lastId ? lastId.id + 1 : 100,
        name: v,
        isDone: false,
      });
    },
    del(id) {
      let idx = this.list.findIndex((v) => v.id == id);
      this.list.splice(idx, 1);
    },
    clear() {
      this.list = this.list.filter((v) => !v.isDone);
    },
  },
  computed: {
    filterList() {
      if (this.status == "未完成") {
        return this.list.filter((v) => !v.isDone);
      } else if (this.status == "已完成") {
        return this.list.filter((v) => v.isDone);
      }
      return this.list;
    },
  },
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("todos80", JSON.stringify(this.list));
      },
    },
  },
};
</script>

<style>
</style>